<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="存放区编号" prop="storeAreaNo">
        <el-input v-model="queryParams.storeAreaNo" placeholder="请输入存放区编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="存放区名称" prop="storeAreaName">
        <el-input v-model="queryParams.storeAreaName" placeholder="请输入存放区名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="库区" prop="whAreaId">
        <el-select v-model="queryParams.whAreaId" placeholder="请选择库区" clearable>
          <el-option v-for="dict in optionss" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['storage:mdStorageAreaInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['storage:mdStorageAreaInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['storage:mdStorageAreaInfo:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mdStorageAreaInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="存放区标识" align="center" prop="storeAreaId" min-width="100px" />
      <el-table-column label="操作标识" align="center" prop="operId" v-if="false" />
      <el-table-column label="存放区编号" align="center" prop="storeAreaNo" min-width="110px" />
      <el-table-column label="存放区名称" align="center" prop="storeAreaName" min-width="180px" />
      <el-table-column label="存放区类别" align="center" prop="sortCode" min-width="110px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sa_sort_code" :value="scope.row.sortCode" />
        </template>
      </el-table-column>
      <el-table-column label="存放区状态" align="center" prop="statusCode" min-width="110px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sa_status_code" :value="scope.row.statusCode" />
        </template>
      </el-table-column>
      <el-table-column label="库房标识" align="center" prop="whId" />
      <el-table-column label="库房名称" align="center" prop="whName" min-width="180px" />
      <el-table-column label="库区标识" align="center" prop="whAreaId" />
      <el-table-column label="库区名称" align="center" prop="whAreaname" min-width="120px" />
      <el-table-column label="存放区类型" align="center" prop="typeCode" min-width="100px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sa_type_code" :value="scope.row.typeCode" />
        </template>
      </el-table-column>
      <el-table-column label="行数" align="center" prop="whRows" />
      <el-table-column label="列数" align="center" prop="whCols" />
      <el-table-column label="运转方式" align="center" prop="operateTypeCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sa_operate_type_code" :value="scope.row.operateTypeCode" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" min-width="170px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['storage:mdStorageAreaInfo:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['storage:mdStorageAreaInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改存放区信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-if="title == '添加存放区信息'">
        <el-form-item label="存放区标识" prop="storeAreaId">
          <el-input v-model="form.storeAreaId" placeholder="请输入存放区标识" />
        </el-form-item>
        <el-form-item label="操作标识" prop="operId" v-if="false">
          <el-input v-model.number="form.operId" placeholder="请输入操作标识" />
        </el-form-item>
        <el-form-item label="存放区编号" prop="storeAreaNo">
          <el-input v-model="form.storeAreaNo" placeholder="请输入存放区编号" />
        </el-form-item>
        <el-form-item label="存放区名称" prop="storeAreaName">
          <el-input v-model="form.storeAreaName" placeholder="请输入存放区名称" />
        </el-form-item>
        <el-form-item label="存放区类别" prop="sortCode">
          <el-select v-model="form.sortCode" placeholder="请选择存放区类别" clearable>
            <el-option v-for="dict in dict.type.sa_sort_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="存放区状态" prop="statusCode">
          <el-select v-model="form.statusCode" placeholder="请选择存放区状态" clearable>
            <el-option v-for="dict in dict.type.sa_status_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库房" prop="whId">
          <el-select v-model="form.whId" placeholder="请选择库房" @change="changeWhId">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="库区" prop="whAreaId">
          <el-select v-model="form.whAreaId" placeholder="请选择库区">
            <el-option v-for="item in option" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="存放区类型" prop="typeCode">
          <el-select v-model="form.typeCode" placeholder="请选择存放区类型" clearable>
            <el-option v-for="dict in dict.type.sa_type_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行数" prop="whRows">
          <el-input v-model.number="form.whRows" placeholder="请输入行数" />
        </el-form-item>
        <el-form-item label="列数" prop="whCols">
          <el-input v-model.number="form.whCols" placeholder="请输入列数" />
        </el-form-item>
        <el-form-item label="运转方式" prop="operateTypeCode">
          <el-select v-model="form.operateTypeCode" placeholder="请选择运转方式" clearable>
            <el-option v-for="dict in dict.type.sa_operate_type_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form ref="forms" :model="forms" :rules="rules" label-width="100px" v-if="title == '修改存放区信息'">
        <el-form-item label="存放区标识" prop="storeAreaId">
          <el-input v-model="forms.storeAreaId" placeholder="请输入存放区标识" disabled />
        </el-form-item>
        <el-form-item label="操作标识" prop="operId" v-if="false">
          <el-input v-model.number="forms.operId" placeholder="请输入操作标识" />
        </el-form-item>
        <el-form-item label="存放区编号" prop="storeAreaNo">
          <el-input v-model="forms.storeAreaNo" placeholder="请输入存放区编号" />
        </el-form-item>
        <el-form-item label="存放区名称" prop="storeAreaName">
          <el-input v-model="forms.storeAreaName" placeholder="请输入存放区名称" />
        </el-form-item>
        <el-form-item label="存放区类别" prop="sortCode">
          <el-select v-model="forms.sortCode" placeholder="请选择存放区类别" clearable>
            <el-option v-for="dict in dict.type.sa_sort_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="存放区状态" prop="statusCode">
          <el-select v-model="forms.statusCode" placeholder="请选择存放区状态" clearable>
            <el-option v-for="dict in dict.type.sa_status_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库房" prop="whId">
          <el-select v-model="forms.whId" placeholder="请选择库房" @change="changeWhId">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="库区" prop="whAreaId">
          <el-select v-model="forms.whAreaId" placeholder="请选择库区">
            <el-option v-for="item in option" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="存放区类型" prop="typeCode">
          <el-select v-model="forms.typeCode" placeholder="请选择存放区类型" clearable>
            <el-option v-for="dict in dict.type.sa_type_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行数" prop="whRows">
          <el-input v-model.number="forms.whRows" placeholder="请输入行数" />
        </el-form-item>
        <el-form-item label="列数" prop="whCols">
          <el-input v-model.number="forms.whCols" placeholder="请输入列数" />
        </el-form-item>
        <el-form-item label="运转方式" prop="operateTypeCode">
          <el-select v-model="forms.operateTypeCode" placeholder="请选择运转方式" clearable>
            <el-option v-for="dict in dict.type.sa_operate_type_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMdStorageAreaInfo, getMdStorageAreaInfo, delMdStorageAreaInfo, addMdStorageAreaInfo, updateMdStorageAreaInfo } from "@/api/storage/mdStorageAreaInfo";
import { selectMdWhInfo, selectMdStorageAreaInfo } from '@/api/storage/mdWhInfo'

export default {
  name: "MdStorageAreaInfo",
  dicts: ['sa_sort_code', 'sa_status_code', 'sa_type_code', 'sa_operate_type_code'],
  data() {
    //自定义验证表单数聚长度
    let operIdValidate = (rule, value, callback) => {
      if (value) {
        if (value.length > 16) {
          callback(new Error("输入长度不得长于16个字符"))
        } else {
          callback()
        }
      } else {
        callback()
      }
    };
    let rowValidate = (rule, value, callback) => {
      if (value) {
        if (value.length > 5) {
          callback(new Error("输入长度不得长于5个字符"))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 存放区信息表格数据
      mdStorageAreaInfoList: [],
      //库区信息
      reservoirArea: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operId: null,
        storeAreaNo: null,
        storeAreaName: null,
        sortCode: null,
        statusCode: null,
        whId: null,
        whAreaId: null,
        typeCode: null,
        whRows: null,
        whCols: null,
        operateTypeCode: null
      },
      // 表单参数
      form: {},
      forms: {},
      // 表单校验
      rules: {
        //存放区标识
        storeAreaId: [{
          required: true,
          message: '存放区标识不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^[1-9]\d{0,15}$/,
          message: '存放区标识不能为0，长度不得超过16位数字'
        },
        ],
        //存放区类别
        sortCode: [{
          required: true,
          message: '存放区类别不能为空',
          trigger: 'change'
        }],
        //存放区名称
        storeAreaName: [{
          required: true,
          message: '存放区名称不能为空',
          trigger: 'blur'
        }, {
          // min: 3,
          max: 256,
          message: '长度不得超过256个字符',
          trigger: 'blur'
        }],
        //存放区状态
        statusCode: [{
          required: true,
          message: '存放区状态不能为空',
          trigger: 'change'
        }],
        //库房标识
        whId: [{
          required: true,
          message: '库房不能为空',
          trigger: 'change'
        }],
        //库区标识
        whAreaId: [{
          required: true,
          message: '库区不能为空',
          trigger: 'change'
        }],
        //操作标识
        operId: [{
          required: false,
          validator: operIdValidate,
          trigger: 'blur'
        }],
        //存放区编号
        storeAreaNo: [{
          required: false,
          validator: operIdValidate,
          trigger: 'blur'
        }],
        //行数
        whRows: [{
          required: false,
          validator: rowValidate,
          trigger: 'blur'
        }],
        //列数
        whCols: [{
          required: false,
          validator: rowValidate,
          trigger: 'blur'
        }],
        //存放区编号
        storeAreaNo: [{
          required: true,
          message: '存放区编号不能为空',
          trigger: 'blur'
        }, {
          // min: 3,
          max: 16,
          message: '长度不得超过16个字符',
          trigger: 'blur'
        }]
      },
      //库房下拉框
      options: [],
      //库区下拉框
      option: [],
      optionss: [],
      //查询库区参数
      querySelsctParams: {
        whId: null
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询存放区信息列表 */
    getList() {
      this.loading = true;
      listMdStorageAreaInfo(this.queryParams).then(response => {
        this.mdStorageAreaInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getSelect()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storeAreaId: null,
        operId: null,
        storeAreaNo: null,
        storeAreaName: null,
        sortCode: null,
        statusCode: null,
        whId: null,
        whAreaId: null,
        typeCode: null,
        whRows: null,
        whCols: null,
        operateTypeCode: null
      };
      this.forms = {
        operId: null,
        storeAreaNo: null,
        storeAreaName: null,
        sortCode: null,
        statusCode: null,
        whId: null,
        whAreaId: null,
        typeCode: null,
        whRows: null,
        whCols: null,
        operateTypeCode: null
      }
      this.resetForm("form");
      this.resetForm("forms")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeAreaId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.sortCode = "01"
      this.form.statusCode = "01"
      this.open = true;
      this.title = "添加存放区信息";
      selectMdWhInfo().then(response => {
        this.options = response.data
        this.form.whId = this.options[0].value
        this.changeWhId()
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const storeAreaId = row.storeAreaId || this.ids
      getMdStorageAreaInfo(storeAreaId).then(response => {
        this.forms = response.data;
        this.open = true;
        this.title = "修改存放区信息";
        selectMdWhInfo().then(response => {
          this.options = response.data
        })
        this.changeSelect()
      });
    },
    /** 提交按钮 */
    submitForm() {
      let mdStorageAreaInfoList = JSON.parse(JSON.stringify(this.mdStorageAreaInfoList))
      if (this.title == "修改存放区信息") {
        updateMdStorageAreaInfo(this.forms).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else if (this.title == "添加存放区信息") {
        let item = mdStorageAreaInfoList.find(value => {
          return value.storeAreaId == this.form.storeAreaId
        })
        if (item == undefined) {
          this.$refs["form"].validate(valid => {
            if (valid) {
              addMdStorageAreaInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          })
        } else {
          this.$message.error('您输入的存放区标识重复，请重新输入');
        }
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeAreaIds = row.storeAreaId || this.ids;
      this.$modal.confirm('是否确认删除存放区信息编号为"' + storeAreaIds + '"的数据项？').then(function () {
        return delMdStorageAreaInfo(storeAreaIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storage/mdStorageAreaInfo/export', {
        ...this.queryParams
      }, `mdStorageAreaInfo_${new Date().getTime()}.xlsx`)
    },
    //级联获取库房下拉框
    changeWhId() {
      this.querySelsctParams.whId = this.form.whId
      let querySelsctParams = JSON.parse(JSON.stringify(this.querySelsctParams))
      selectMdStorageAreaInfo(querySelsctParams).then(response => {
        this.option = response.data
      })
    },
    //修改数据库区下拉框
    changeSelect() {
      this.$forceUpdate()
      this.querySelsctParams.whId = this.form.whId || this.forms.whId
      let querySelsctParams = JSON.parse(JSON.stringify(this.querySelsctParams))
      selectMdStorageAreaInfo(querySelsctParams).then(response => {
        this.option = response.data
      })
    },
    //查询参数库房下拉框
    getSelect() {
      let querySelsctParams = {}
      querySelsctParams.whId = 1
      selectMdStorageAreaInfo(querySelsctParams).then(response => {
        this.optionss = response.data
      })
    }
  }
};
</script>
